// pages/lock/lock.js
import weapp from '../../components/weapp/index';

var encryp = require('../../assets/js/encryp.js')
var wxRequest = require('../../assets/js/wxRequest.js')
var common = require('../../assets/js/common.js')
var base64 = require('../../assets/js/base64.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentPage: 1,      //当前页
    token: '',
    randomKey: '',
    sign: '',
    userId: '',
    deptArray: [],   //区域数据(包含区)
    dept:[],         //用户选择的区域
    houseArray: [],  //库房
    equmentArray: [], //设备
    house: [],       //当前选择的库房
    equment: [],      //当前选择的设备
    array1: [[], []],
    index1: [],
    array2: ['变电站', '开闭所', '环网柜', '分接箱', 'JP柜', '箱变', '配电室', '专变','低压计量箱'],
    index2: 0,
    array3: ['设备'],
    index3: 0,
    lockArray: [],  //锁列表

    showSearchAndPage: false,
    showLoadMore: false,  //是否显示加载更多动画

    icons: base64.equementDefault,

    hasClickPickType: false     //用户是否滚动设备类型列表
  },
  /**
   * 根据用户获取区域
   */
  getDept: function (e) {
    var that = this;
    var simpleObject = {
      id: that.data.userId
    };
    var param = common.packParam(that.data.randomKey, simpleObject);
    var url = getApp().globalData.httpUrl + '/dept/byUserlist';
    var info = wxRequest.postRequest(url, param, that.data.token).
      then(res => {
        var data = {
          array1: that.data.array1
        };
        //把区域列表设置到piker组件
        data.array1[0].push(res.data.title)
        var children = res.data.children;
        //该用户属于市下
        var flag = common.isArray(children)
        if (flag) {
          //仅设置区的数据(默认用户只属于一个市)
          that.setData({
            deptArray: children
          })
          for (var i = 0; i < children.length; i++) {
            data.array1[1].push(children[i].title);
          }
        } else {
          that.setData({
            deptArray: res.data
          })
        }
        this.setData(data)
      })
  },
  /**
   * 获取设备
   */
  getEqument: function (deptId, offset,types) {
    var that = this;
    var simpleObject = {
      id: deptId,
      type: types,
      offset: offset,
      limit: getApp().globalData.limit
    };
    var param = common.packParam(that.data.randomKey, simpleObject);
    var url = getApp().globalData.httpUrl + '/equipment/getEquipmentByDept';
    var info = wxRequest.postRequest(url, param, that.data.token).
      then(res => {
        if (res.data == '' || res.data == []) {
          that.setData({
            equmentArray: res.data,
          })
          this.oLoader.setStatus('empty');
        } else {
          this.oLoader.setStatus('');
          that.setData({
            equmentArray: res.data,
            showSearchAndPage: true,  //显示搜索框和页码
            showLoadMore: false   //隐藏加载更多动画
          })
        }

      })
  },
  /**
   * 区域列表滚动事件
   */
  bindPickerChange1: function (e) {
    var that = this;
    var index = e.detail.value
    //选择区域后获取库房数据
    var jsons = that.data.deptArray;
    if (jsons.length) {
      var dept = that.data.deptArray[index[1]];  //有两个
    } else {          //只有一个区，不需要滚动选择
      var dept = that.data.deptArray;
    }
    that.setData({
      dept: dept
    })
    if (that.data.hasClickPickType) {  //用户选择过设备类型
      that.getEqument(that.data.dept.id, that.data.currentPage, that.data.index2)
    } else {
      that.getEqument(that.data.dept.id, that.data.currentPage)
    }
  },
  /**
   * 设备类型列表滚动事件
   */
  bindPickerChange2: function (e) {
    var that = this;
    console.log(e.detail.value)
    var index = parseInt(e.detail.value)
    that.setData({
      hasClickPickType: true,     //用户滚动列表标记
      index2: index,
      currentPage: 1,
      icons: base64.equementIcon[index]
    })
    that.getEqument(that.data.dept.id, that.data.currentPage, that.data.index2)
  },
  equeInfo: function(e){
    var that = this;
    var index = parseInt(e.currentTarget.dataset.index)
    console.log(index)
    let equeInfo = JSON.stringify(that.data.equmentArray[index]);

    wx.navigateTo({
      url: '/pages/equement_info/equement_info?equeInfo=' + equeInfo,
    })
  },
  /**
   * 上一页
   */
  preView: function () {
    var that = this;
    if (that.data.currentPage <= 1) {
      return;
    }
    //页数加1
    var page = that.data.currentPage - 1;
    that.setData({
      showLoadMore: true,   //显示加载更多动画
      currentPage: page
    })
    if (that.data.hasClickPickType){  //用户选择过设备类型
      that.getEqument(that.data.dept.id, that.data.currentPage, that.data.index2)
    }else{
      that.getEqument(that.data.dept.id, that.data.currentPage)
    }
    
  },
  /**
   * 下一页
   */
  nextView: function () {
    var that = this;
    //页数加1
    var page = that.data.currentPage + 1;
    that.setData({
      showLoadMore: true,   //显示加载更多动画
      currentPage: page
    })
    if (that.data.hasClickPickType) {  //用户选择过设备类型
      that.getEqument(that.data.dept.id, that.data.currentPage, that.data.index2)
    } else {
      that.getEqument(that.data.dept.id, that.data.currentPage)
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    // 每个页面都需要先获取缓存中的token和security
    var tokens = wx.getStorageSync('token')
    var randomKey = wx.getStorageSync('randomKey')
    var sign = wx.getStorageSync('sign')
    var id = wx.getStorageSync('id')
    if (tokens && randomKey) {
      that.setData({
        token: tokens,
        randomKey: randomKey,
        sign: sign,
        userId: id
      })
    }
    that.getDept();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that = this;
    this.oLoader = weapp.Loader();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }

})